package com.zhj.common.base;

import org.springframework.http.HttpStatus;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * 响应体
 */
@Data
@AllArgsConstructor
public class Result<T> {

    /** Code */
    private int code;

    /** Message */
    private String message;

    /** Data */
    private T data;


    /**
     * Success
     *
     * @param <T>  parameter
     * @param data data
     * @return the result
     * @since 1.0.0
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), data);
    }

    /**
     * Fail
     *
     * @param <T>     parameter
     * @param message message
     * @return the result
     * @since 1.0.0
     */
    public static <T> Result<T> fail(String message) {
        return new Result<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), message, null);
    }

    /**
     * Fail
     *
     * @param <T>     parameter
     * @param message message
     * @param code    code
     * @return the result
     * @since 1.0.0
     */
    public static <T> Result<T> fail(String message, int code) {
        return new Result<>(code, message, null);
    }

    /**
     * Fail
     *
     * @param <T> parameter
     * @return the result
     * @since 1.0.0
     */
    public static <T> Result<T> fail() {
        return new Result<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), null);
    }

}
